package cn.zjxiliu.lib_core.http;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by jessie on 2018/10/25.
 */

public class HttpCore {
    private static final String TAG = "HttpCore";

    private static final HttpCore httpCore;

    private String baseRequestUrl;

    private Gson gson = new Gson();


    static {
        httpCore = new HttpCore();
    }

    private HttpCore(){}

    public static HttpCore getInstance(){
        return httpCore;
    }

    public void init(String baseRequestUrl){
        this.baseRequestUrl = baseRequestUrl;
    }


    private String formatUrl(String path){
        if(TextUtils.isEmpty(path) || TextUtils.isEmpty(baseRequestUrl)){
            try {
                throw new Exception("path为空或baseRequestUrl为空");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return baseRequestUrl + path;
    }


    private <T extends BaseRequest> RequestParams getRequestParams(T requestBean, String requestPath){
        String requestUrl = null;
        if(requestPath.startsWith("http")){
            requestUrl = requestPath;
        }else{
            requestUrl = formatUrl(requestPath);
        }
        RequestParams params = new RequestParams(requestUrl);
        params.setBodyContent(gson.toJson(requestBean));
        params.setAsJsonContent(true);
        params.setHeader("Content-Type", "application/json");
        Logger.d("requestUrl:" + params.getUri() + " requestContent:" + params.getBodyContent());
        return params;
    }

    public <T extends BaseRequest, S> ResponseBean<S> postSyncSend(T requestBean, String requestPath, SyncRequestBackBean<S> backBean) throws Throwable {
        RequestParams params = getRequestParams(requestBean, requestPath);
        String result = x.http().postSync(params, String.class);
        return backBean.getResult(result);
    }


    public <T extends BaseRequest, S> Callback.Cancelable postSend(T requestBean, String requestPath, final RequestCallback<S> callback){
        RequestParams params = getRequestParams(requestBean, requestPath);
        Callback.Cancelable cancelable = x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Logger.i(TAG, "onSuccess:" + result);
                if(null != callback)
                    callback.onSuccessCall(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Logger.e(TAG, "onError:" + ex.getStackTrace());
                if(null != callback)
                    callback.onErrorCall(ex, isOnCallback);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                if(null != callback)
                    callback.onCancelledCall(cex);
            }

            @Override
            public void onFinished() {
                if(null != callback)
                    callback.onFinishedCall();
            }
        });
        return cancelable;
    }

}
